class Solution {
public:
    string longestPalindrome(string s) {
        if (s.size() == 0) { return ""; }

        int begin = 0;
        int maxLen = 1;
        for (int i = 0; i < s.size() - 1; ++i) {
            for (int j = i + 1; j < s.size(); ++j) {
                if (j - i + 1 > maxLen && isPalindrome(s, i, j)) {
                    begin = i;
                    maxLen = j - i + 1;
                }
            }
        }
        return s.substr(begin, maxLen);
    }
    bool isPalindrome(string &s, int begin, int end) {
        while (begin < end) {
            if (s[begin] != s[end]) {
                return false;
            }
            begin++;
            end--;
        }
        return true;
    }
};
